<template>
  <div>
    <!-- 已驳回 4 -->
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="驳回原因"
      :visible="open"
      width="500px"
      append-to-body
      @close="cancel()"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="type == 1 || type == 2 ? rules : {}"
        label-width="80px"
      >
        <el-form-item v-if="type == 1 || type == 2" label="驳回原因" prop="rejectReason">
          <el-input
            v-model="form.rejectReason"
            maxlength="100"
            show-word-limit
            :rows="3"
            type="textarea"
            placeholder="请输入驳回原因"
          />
        </el-form-item>
        <div v-else style="white-space: pre-wrap"
        >{{ form.rejectReason }}
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer" style="display: flex;justify-content: flex-end">
        <el-button v-preventReClick v-if="type == 1 || type == 2" @click="cancel">取 消</el-button>
        <el-button
          v-preventReClick
          type="primary"
          @click="submitForm"
          v-if="type == 1 || type == 2"
        >确 定
        </el-button>
        <el-button
          v-preventReClick
          @click="cancel"
          v-if="type == 3"
        >关 闭
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { editCourseStatus, getCourseDetial } from '@/api/course/course'

export default {
  props: {
    open: Boolean,
    type: [Number, String],
    propform: {
      type: [Object],
      default: {}
    }
  },
  data() {
    return {
      // 表单参数
      form: {
        rejectReason: undefined
      },
      // 表单校验
      rules: {
        rejectReason: [
          { required: true, message: '驳回原因不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    open(e) {
      if (e) {
        if (this.type == 1) {
          this.reset()
        }
        if (this.type == 3) {
          getCourseDetial(this.propform.courseId).then((res) => {
            this.form = res.data
          })
        }
      }
    }
  },
  mounted() {
  },
  methods: {
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          //type  类型:0-高用,1-停用,2-提交审校,了-撤回审校,4-分院审校,5-学校审校,6-驳回
          let params = {
            type: 6,
            courseId: this.propform.courseId
          }
          const formData = new FormData()
          formData.append('rejectReason', this.form.rejectReason)
          let res = await editCourseStatus(params.type, params.courseId, formData)
          if (res.code == 200) {
            this.$emit('update:open', false)
            this.$emit('refresh', false)
            this.$modal.msgSuccess('操作成功')
          }
        }
      })
    },
    // 表单重置
    reset() {
      this.form = {
        rejectReason: undefined
      }
      this.resetForm('form')
    },
    cancel() {
      this.$emit('update:open', false)
    }
  }
}
</script>
<style scoped lang="scss"></style>
